package cn.springbak.publish.web.interceptor;

import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Method;
import java.time.LocalTime;
import java.util.Map;

@Slf4j    //帮助写通用型的对象不需要自己去写
public class TimeAccessInterceptor implements HandlerInterceptor {
    //private static  final Logger log = LoggerFactory.getLogger(TimeAccessInterceptor.class);
    public boolean preHandle(HttpServletRequest request,HttpServletResponse response,Object handler) throws Exception{
        LocalTime now = LocalTime.now();
        int hour = now.getHour();
        log.info("hour {}", hour);
        if(hour>6){
            throw new RuntimeException("xxxxx");
            return true;
        }
    private void testRequestInfo(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception{
        Map<String,String[]> map = request.getParameterMap();
            System.out.println(map.keySet());
            HandlerMethod handlerMethod = (HandlerMethod) handler;
            Method method = handlerMethod.getMethod();
            System.out.println(method.getName());
    }


/*
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        LocalTime now = LocalTime.now();
        int hour = now.getHour();
        log.info("hour{}",hour);
        if(hour<6||hour>20)
            throw new RuntimeException("请在6-10点进行访问");
            return true;
    }

    private void testRequestInfo(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        Map<String,String[]> map = request.getParameterMap();
        System.out.println("request.param.map.keys="+map.keySet());
        //获取封装了目标handler和method信息的handlermethod对象
        HandlerMethod handlerMethod = (HandlerMethod) handler;
        Method method = handlerMethod.getMethod();
        System.out.println(method.getName());

        System.out.println("==prehandler==");


    }*/

}